package com.application.lucky.utils;

import android.app.ActivityManager;
import android.content.Context;
import android.view.Window;

import com.application.mylibrary.utils.SharedPreferencesUtil;

/**
 * @author lixiaoyu
 * @version v1.0
 * @description 公共common类
 * @since 2017/11/21
 */

public class CommonUtils {

    // 两次点击按钮之间的点击间隔不能少于800毫秒
    private static final int MIN_CLICK_DELAY_TIME = 700;
    private static long lastClickTime;

    /**
     * 防重复点击
     *
     * @return
     */
    public static boolean isNotFastClick() {
        boolean flag = false;
        long curClickTime = System.currentTimeMillis();
        if ((curClickTime - lastClickTime) >= MIN_CLICK_DELAY_TIME||(curClickTime - lastClickTime)<0) {
            lastClickTime = curClickTime;
            flag = true;
        }
        return flag;
    }

    public static boolean isNotFastClick(int intervalTime) {
        if(intervalTime < 0)
            intervalTime = MIN_CLICK_DELAY_TIME;
        boolean flag = false;
        long curClickTime = System.currentTimeMillis();
        if ((curClickTime - lastClickTime) >= intervalTime||(curClickTime - lastClickTime)<0) {
            lastClickTime = curClickTime;
            flag = true;
        }
        return flag;
    }

    public static boolean isNotFastClick(Context context, String actionName, long protectedTime) {
        boolean flag = false;
        if (context == null) {
            return flag;
        }
        String lastString = SharedPreferencesUtil.getString(context, actionName, "0");
        long lastTime = Long.parseLong(lastString);
        long curClickTime = System.currentTimeMillis();
        if ((curClickTime - lastTime) >= protectedTime||(curClickTime - lastTime)<0) {
            SharedPreferencesUtil.saveString(context, actionName, curClickTime + "");
            flag = true;
        }
        return flag;
    }

    public static String getCurProcessName(Context context) {
        int pid = android.os.Process.myPid();
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningAppProcessInfo appProcess : activityManager.getRunningAppProcesses()) {
            if (appProcess.pid == pid) {
                return appProcess.processName;
            }
        }
        return null;
    }

    /**
     * 隐藏虚拟按键，并且设置成全屏
     */
    public static void setHideVirtualKey(Window window) {
//        int uiOptions =
//                // 防止系统栏隐藏时内容区域大小发生变化
//                View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
//                        //布局位于状态栏下方
//                        View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
//                        //隐藏导航栏
//                        View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
//                        // 沉浸式STICKY模式，将 bar 唤出后几秒就消失
//                        View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
//
//        if (Build.VERSION.SDK_INT >= 19) {
//            uiOptions = uiOptions | 0x00001000;
//        } else {
//            uiOptions = uiOptions | View.SYSTEM_UI_FLAG_LOW_PROFILE;
//        }
//
//        window.getDecorView().setSystemUiVisibility(uiOptions);
    }
}
